<template>
  <div>
      图表
    <div ref="barDiv" style="width: 600px;height:400px;"></div>

    <div ref="pieDiv" style="width: 600px;height:400px;"></div>

    <div ref="manyBarDiv" style="width: 600px;height:400px;"></div>

  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  data() {
    return {
      productType:['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
      sellNum: [5, 20, 36, 10, 10, 20],
      gradeList: [
        { value: 5, name: '90以上' },
        { value: 4, name: '80-90' },
        { value: 12, name: '60-80' },
        { value: 6, name: '60以下' }
      ]
    }
  },
  methods:{
    createBar(){//柱状图
      var myChart = echarts.init(this.$refs.barDiv);

      // 指定图表的配置项和数据
      var option = {
        title: {
          text: '销量统计'
        },
        tooltip: {},
        legend: {
          data: ['销量']
        },
        xAxis: {
          data: this.productType
        },
        yAxis: {},
        series: [
          {
            name: '销量',
            type: 'bar',
            data: this.sellNum
          }
        ]
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },

    createPie(){//饼图
      var pieChart = echarts.init(this.$refs.pieDiv);
      var option = {
        title: {
          text: '学生成绩分布',
          subtext: '2022-07-22',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '成绩分布',
            type: 'pie',
            radius: '50%',
            data: this.gradeList,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      };
      pieChart.setOption(option);
    },

    createManyBar(){
      var manyBarChart = echarts.init(this.$refs.manyBarDiv);

      const posList = [
        'left',
        'right',
        'top',
        'bottom',
        'inside',
        'insideTop',
        'insideLeft',
        'insideRight',
        'insideBottom',
        'insideTopLeft',
        'insideTopRight',
        'insideBottomLeft',
        'insideBottomRight'
      ];
      app.configParameters = {
        rotate: {
          min: -90,
          max: 90
        },
        align: {
          options: {
            left: 'left',
            center: 'center',
            right: 'right'
          }
        },
        verticalAlign: {
          options: {
            top: 'top',
            middle: 'middle',
            bottom: 'bottom'
          }
        },
        position: {
          options: posList.reduce(function (map, pos) {
            map[pos] = pos;
            return map;
          }, {})
        },
        distance: {
          min: 0,
          max: 100
        }
      };
      app.config = {
        rotate: 90,
        align: 'left',
        verticalAlign: 'middle',
        position: 'insideBottom',
        distance: 15,
        onChange: function () {
          const labelOption = {
            rotate: app.config.rotate,
            align: app.config.align,
            verticalAlign: app.config.verticalAlign,
            position: app.config.position,
            distance: app.config.distance
          };
          myChart.setOption({
            series: [
              {
                label: labelOption
              },
              {
                label: labelOption
              },
              {
                label: labelOption
              },
              {
                label: labelOption
              }
            ]
          });
        }
      };
      const labelOption = {
        show: true,
        position: app.config.position,
        distance: app.config.distance,
        align: app.config.align,
        verticalAlign: app.config.verticalAlign,
        rotate: app.config.rotate,
        formatter: '{c}  {name|{a}}',
        fontSize: 16,
        rich: {
          name: {}
        }
      };
      var option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {
          data: ['食品类', '酒类', '药品类', '图书类']
        },
        toolbox: {
          show: true,
          orient: 'vertical',
          left: 'right',
          top: 'center',
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar', 'stack'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        xAxis: [
          {
            type: 'category',
            axisTick: { show: false },
            data: ['2012', '2013', '2014', '2015']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '食品类',
            type: 'bar',
            barGap: 0,
            label: labelOption,
            emphasis: {
              focus: 'series'
            },
            data: [2000, 1500, 1800, 2300]
          },
          {
            name: '酒类',
            type: 'bar',
            label: labelOption,
            emphasis: {
              focus: 'series'
            },
            data: [500, 290, 328, 459]
          },
          {
            name: '药品类',
            type: 'bar',
            label: labelOption,
            emphasis: {
              focus: 'series'
            },
            data: [1400, 1300, 1280, 1340]
          },
          {
            name: '图书类',
            type: 'bar',
            label: labelOption,
            emphasis: {
              focus: 'series'
            },
            data: [985,810,768,840]
          }
        ]
      };
      manyBarChart.setOption(option);

    }
  },
  mounted() {
    this.createBar();
    this.createPie();
    this.createManyBar();
  }
}
</script>

<style scoped>

</style>